home *** CD-ROM | disk | FTP | other *** search
- Path: engnews2.Eng.Sun.COM!usenet
- From: nitin@more.eng.sun.com (Nitin More [CONTRACTOR])
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] Dynamically allocating memory for a char*
- Date: 19 Jan 1996 17:50:12 GMT
- Organization: SunSoft
- Message-ID: <NITIN.96Jan19095012@more.eng.sun.com>
- References: <4dmn1i$10t@walrus2.walrus.com>
- NNTP-Posting-Host: more.eng.sun.com
- In-reply-to: fjordao@walrus.com's message of Fri, 19 Jan 1996 00:00:49 GMT
-
-
- To read a string from cin, you need a place to store it (char *) with enough
- memory allocated. Until you read the string, you won't know what is enough.
- This is a catch-22. The best you can do is use a temporary variable with
- (reasonable) maximum possible size, read the string in the temporary variable,
- get the size plus one for the NULL character, allocate that much memory
- pointed to by your original variable and copy the string into it from the
- temporary variable.
-
- Since you may get into situation where the input string is bigger than the
- maximum size you have chosen, you could do the following. Set the last char
- in the temporary variable to be NULL (i.e. 0) and check if it is still the
- same after reading the string. If it is same then you are safe. If not, the
- string read is bigger than your maximum size and you don't have the complete
- string. [Worse yet, you might have overwritten memory of some other
- variable]. Either you can display an error and quit or reallocate bigger size
- (e.g., 2*maximum) for the temporary and try again. Try until you succeed.
-
- Anybody else knows a better way?
-
- -Nitin
-
-
- In article <4dmn1i$10t@walrus2.walrus.com> fjordao@walrus.com (Felipe Jordao) writes:
-
- > From: fjordao@walrus.com (Felipe Jordao)
- > Newsgroups: comp.lang.c++
- > Date: Fri, 19 Jan 1996 00:00:49 GMT
- > Organization: HAC
- >
- > Hi,
- >
- > I have a question about allocating memory for a string as it's passed
- > in by the user. I'm using cin to get the info, but maybe this is
- > inappropriate. I have included the relevant snippets of the file
- > below. What I am trying to achieve is no limitation for the user when
- > they input a path + filename. Is it possible to use char* foo and
- > then something like (I know this is wrong, but the idea of it ...)
- >
- > cin >> malloc(sizeof(infile)); :0
- >
- > Here is the actual code
- > ----------------------------------------------------------------------
- >
- > main()
- > {
- > ifstream InputFile; // Input file stream
- > ofstream OutputFile; // Output file stream
- > .
- > .
- > .
- > OpenFiles(InputFile, OutputFile);
- > .
- > .
- > }
- >
- >
- > void OpenFiles(ifstream& InputFile, ofstream& OutputFile)
- > {
- > char infile[30]; // <---- RIGHT HERE. this works as it is,
- > char outfile[30]; // but I would like to use
- > // char *infile, *outfile. When
- > // I try, I have not found a way to
- > // dynamically allocate memory when the
- > // user inputs the file. I know I can
- > // use something like infile = new char[X]
- > // but that still leave me with the X limit.
- >
- > cout << "Enter the path and filename of" << endl
- > << "the input file (*.csv): ";
- > cin >> infile; // I would like to allocate memory right after
- > // user inputs the name of the file. If I use
- > // *infile and leave it like this it crashes
- > // because of unallocated space
- >
- > InputFile.open(infile); // Test to see if the input file opens
- > if (!InputFile) {
- > cerr << "Cannot open input file!" << endl;
- > exit(1);
- > }
- >
- >
- > cout << "Enter the path and filename of" << endl // get output file
- > << "the output file (*.bcp): "; // from user
- > cin >> outfile;
- >
- > OutputFile.open(outfile); // Test to see if the output file opens
- > if (!OutputFile) {
- > cerr << "Cannot open output file!" << endl;
- > exit(1);
- > }
- > }
- >
- --
- ----------------------------------------------------------------------
- Nitin More
- SunSoft, Bldg 16 Off: (415) 786 7109
- Menlo Park, CA Fax: (415) 786 7957 e-mail: nitin@more.eng.sun.com
- ----------------------------------------------------------------------
-